from datetime import datetime

from django.contrib.auth import get_user_model
from django.db import models


User = get_user_model()
class Goods(models.Model):
    """
    商品表
    """
    putaway_choices = (
        (1,"上架"),
        (2,"下架")
    )
    name = models.CharField(max_length=30, verbose_name="商品名")
    current_price = models.CharField(max_length=20, default=0, verbose_name="商品原价")
    original_price = models.CharField(max_length=20, default=0, verbose_name="商品现价")
    sales = models.IntegerField(default=0, verbose_name='销量')
    slave = models.IntegerField(default=0, verbose_name='库存')
    putaway = models.CharField(max_length=20,choices=putaway_choices, default=1, verbose_name="商品状态")
    front_image = models.CharField(max_length=255,null=True, verbose_name="首页图片")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="添加时间")

    class Meta:
        db_table = 'tb_goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class GoodsCateogory(models.Model):
    """
    商品分类
    """
    name = models.CharField(max_length=10, verbose_name="分类名称")
    image = models.CharField(max_length=255,null=True, verbose_name="封面图")
    parent_category = models.ForeignKey('self', null=True, blank=True, verbose_name="父级目录", help_text="父级目录",
                                        related_name="sub_cat")  # 自关联父级
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="添加时间")
    class Meta:
        db_table = 'tb_goods_cateogory'
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Category(models.Model):
    """
    分类
    """

    goods = models.ForeignKey(Goods, verbose_name="商品")
    category = models.ForeignKey(GoodsCateogory,verbose_name="商品分类" )
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="添加时间")

    class Meta:
        db_table = 'tb_category'
        verbose_name = '分类表'
        verbose_name_plural = verbose_name

class GoodsImage(models.Model):
    """
    商品详情图
    """
    goods = models.ForeignKey(Goods, related_name="img_goods", verbose_name="images")
    image = models.ImageField(upload_to="", null=True, blank=True, verbose_name="图片")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        db_table = 'tb_goods_image'
        verbose_name = "商品图片"
        verbose_name_plural = verbose_name



    def __str__(self):
        return self.goods.name


#